package cn.itcast.mq;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.concurrent.TimeUnit;

@Component
@Slf4j
public class SpringRabbitListener {

    private static  long work1num = 0;
    private static  long work2num = 0;
   /* @RabbitListener(queues = "simple.queue")
    public void listenerSimpleMsg(String msg){
        log.info("消费者接收到消息"+msg);
    }*/
   @RabbitListener(queues = "simple.queue")
   public void listenerSimpleMsg1(String msg) throws InterruptedException {
       work1num++;
       log.info("消费者1接收到消息"+msg,work1num);
       TimeUnit.MICROSECONDS.sleep(20);
   }
    @RabbitListener(queues = "simple.queue")
    public void listenerSimpleMsg2(String msg) throws InterruptedException {
       work2num++;
        log.info("消费者2接收到消息"+msg,work2num);
        TimeUnit.MICROSECONDS.sleep(200);
    }

    @RabbitListener(queues = "fanout.queue1")
    public void listenerfanoutMsg1(String msg) throws InterruptedException {
        //work1num++;
        log.info("消费者1接收到fanout消息"+msg,work1num);
        //TimeUnit.MICROSECONDS.sleep(20);
    }
    @RabbitListener(queues = "fanout.queue2")
    public void listenerfanoutMsg2(String msg) throws InterruptedException {
        //work2num++;
        log.info("消费者2接收到fanout消息"+msg,work2num);
        //TimeUnit.MICROSECONDS.sleep(200);
    }


    @RabbitListener(bindings = @QueueBinding(
            value = @Queue("direct.queue1"),
            exchange = @Exchange(value = "itcast.direct",type = ExchangeTypes.DIRECT), // 交换机
            key = {"blue","red"}
    ))
    public void listenDirectQueue01(String msg) throws InterruptedException {
        log.warn("消费者1 接收到消息", msg);
    }
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue("direct.queue2"),
            exchange = @Exchange(value = "itcast.direct",type = ExchangeTypes.DIRECT), // 交换机
            key = {"yellow","red"}
    ))
    public void listenDirectQueue02(String msg) throws InterruptedException {
        log.warn("消费者2 接收到消息", msg);
    }



    @RabbitListener(bindings = @QueueBinding(
            value = @Queue("topic.queue1"),
            exchange = @Exchange(value = "itcast.topic",type = ExchangeTypes.TOPIC), // 交换机
            key = {"china.#"}
    ))
    public void listenTopicQueue01(String msg) throws InterruptedException {
        log.warn("消费者1 接收到消息", msg);
    }
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue("topic.queue2"),
            exchange = @Exchange(value = "itcast.topic",type = ExchangeTypes.TOPIC), // 交换机
            key = {"#.news"}
    ))
    public void listenTopicQueue02(String msg) throws InterruptedException {
        log.warn("消费者2接收到消息", msg);
    }
    @RabbitListener(queues = "object.queue")
    public void listenObjectQueue01(Map map) throws InterruptedException {
        log.warn("消费者1接收到消息", map);
    }
}
